package com.fancon.android.gardenjokesx.dialog;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.fancon.android.gardenjokesx.GardenJokesBaseActivity;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.activity.ProfileActivity;
import com.fancon.android.gardenjokesx.entity.ReportErrors;
import com.fancon.android.gardenjokesx.rpc.GardenJokesApiConnect;
import com.fancon.android.gardenjokesx.rpc.JsonPaser;
import com.fancon.android.gardenjokesx.utils.UserUtil;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.ui.widget.FanconProgressBar;

public class UserReportingDialog extends Dialog{

	Context mContext;
	String viewedUserId;
	Button btnCancel, btnReport, btnBlock;
	Boolean isBlocked;
	public UserReportingDialog(Context context, String viewedUser, Boolean isblocked) {
		super(context, android.R.style.Theme_Translucent_NoTitleBar);
		setContentView(R.layout.user_reporting_dialog);
		mContext = context;
		viewedUserId = viewedUser;
		isBlocked = isblocked;
		btnCancel = (Button)findViewById(R.id.cancel);
		btnReport = (Button)findViewById(R.id.report_user);
		btnBlock = (Button)findViewById(R.id.block_user);
		if (isblocked) {
			btnBlock.setText(mContext.getResources().getString(R.string.unblock_user));
		} else {
			btnBlock.setText(mContext.getResources().getString(R.string.block_user));
		}
		
		
		// TODO Auto-generated constructor stub
		// Get resion for sending
		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Do nothing, just for hide dialog
				dismiss();
			}
		});

		btnReport.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Send the reason report user to server
				DialogReportJorke reportUser = new DialogReportJorke(mContext, null, viewedUserId, true);
				reportUser.show();
				dismiss();
			}
		});
		btnBlock.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Send the reason report user to server
				AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
				builder.setMessage(mContext.getResources().getString(R.string.block_user))
						.setCancelable(false)
						.setNegativeButton(mContext.getResources().getString(R.string.common_calcel),
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										dialog.cancel();
										UserReportingDialog.this.dismiss();
									}
								})
						.setPositiveButton(mContext.getResources().getString(R.string.common_ok),
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										// Block user.
										doBlockUser(isBlocked ? "false" : "true");
										dialog.cancel();
									}
								});
				AlertDialog alert = builder.create();
				alert.show();
				
			}
		});
	}
	
	public void doBlockUser(String isBlock){
		BlockUserRequest block = new BlockUserRequest("m04block", (GardenJokesBaseActivity) mContext, isBlock);
		((GardenJokesBaseActivity)mContext).getGlobalState().getRequestQueue().addRequest(block);
	}
	public class BlockUserRequest extends RequestUI{
		private FanconProgressBar progress;
		private String isBlock;
		private ReportErrors report;
		public BlockUserRequest(Object key, Activity activity,String isBlock) {
			super(key, activity);
			this.isBlock = isBlock;
			// TODO Auto-generated constructor stub
			//progress = MeshProgressBar.show(context, context.getString(R.string.common_loading));
		}

		@Override
		public void execute() throws Exception {
			// TODO Auto-generated method stub
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			nameValuePairs.add(new BasicNameValuePair("blocker_id", UserUtil.getInfoUserLogin(mContext).getUser_id()));
			nameValuePairs.add(new BasicNameValuePair("current_user_id", viewedUserId));
			nameValuePairs.add(new BasicNameValuePair("is_block", isBlock));
			GardenJokesApiConnect api = new GardenJokesApiConnect(mContext);
			String result = api.execGet(mContext, GardenJokesApiConnect.GROUP_M, "blockUser",
					nameValuePairs);
			Log.v("StringResult", result);
			JsonPaser jP = new JsonPaser();
			report = jP.getReport(result);
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			if(progress!=null){
				progress.dismiss();
			}
			if(report != null){
				if(report.isIs_success()){
					((ProfileActivity)(mContext)).setIsBlocked(!isBlocked);
				}
			}
			UserReportingDialog.this.dismiss();
			
		}
		
	}

}
